package com.everflourish.yeah100.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.everflourish.yeah100.R;

public class CircleImageView extends ImageView {

    private Xfermode mXfermode = null;
    private Paint mPaint = null;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mXfermode = new PorterDuffXfermode(Mode.DST_IN);
        mPaint = new Paint();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        // 定义画布的大小，一般情况下，宽跟高是相等的，如果不相等，取小的值。
        int r = getHeight() > getWidth() ? getWidth() : getHeight();
        // 创建一张空白的图片
        Bitmap bitmap = Bitmap.createBitmap(r, r, Config.ARGB_8888);
        // 创建Canvas：如果把Canvas比作画师，那么bitmap就好一张画纸。
        Canvas tempCanvas = new Canvas(bitmap);

        // 定义背景颜色
        mPaint.setColor(getResources().getColor(R.color.body_bg));
        tempCanvas.drawRect(0, 0, r, r, mPaint);

        Drawable drawable = getDrawable();
        if (drawable != null) {
            int drawableW = drawable.getIntrinsicWidth();
            int drawableH = drawable.getIntrinsicHeight();
            // 压缩图片：等比例压缩
            int drawableMin = drawableH > drawableW ? drawableW : drawableH;
            float scale = r / (float) drawableMin;
            drawable.setBounds(0, 0, (int) (scale * drawableW),
                    (int) (scale * drawableH));
            // 第一层...目标层
            drawable.draw(tempCanvas);
        }
        mPaint.reset();
        mPaint.setFilterBitmap(true);
        mPaint.setAntiAlias(true);
        mPaint.setXfermode(mXfermode);
        // 第二层...遮罩层
        tempCanvas.drawBitmap(makeSrc(r), 0, 0, mPaint);
        // mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP));
        // 将画好的bitmap图片直接显示出来就行了
        canvas.drawBitmap(makeSrc2(r), 0, 0, null);
        canvas.drawBitmap(bitmap, 0, 0, null);
    }

    /**
     * 将该Bitmap作为遮罩层（圆）
     *
     * @param r
     * @return
     */
    private Bitmap makeSrc(int r) {
        int margin = (int) getResources().getDimension(R.dimen.portrait_margin);
        Bitmap bitmap = Bitmap.createBitmap(r, r, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        Paint p = new Paint();
        p.setAntiAlias(true);// 消除锯齿
        p.setColor(Color.WHITE);
        c.drawCircle(r / 2, r / 2, (r / 2) - margin, p);
        return bitmap;
    }

    /**
     * 将该Bitmap作为遮罩层（圆）
     *
     * @param r
     * @return
     */
    private Bitmap makeSrc2(int r) {
        Bitmap bitmap = Bitmap.createBitmap(r, r, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        Paint p = new Paint();
        p.setAntiAlias(true);// 消除锯齿
        p.setColor(0x55a6caff);
        c.drawCircle(r / 2, r / 2, r / 2, p);
        return bitmap;
    }
}